home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / PARSE2.C < prev    next >
C/C++ Source or Header  |  1993-06-11  |  3KB  |  98 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville MI
  3. Date: 06-06-93 (09:15)             Number: 57
  4. From: JERRY COFFIN                 Refer#: 142
  5.   To: ROLLIN WHITE                  Recvd: NO  
  6. Subj: Parsing a Text file            Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8. On (03 Jun 93) Rollin White wrote to All...
  9.  
  10.  RW>         Does anyone have code(or even a reccommended approach) fo
  11.  RW> reading in a text file that will serve as a program's configuration
  12.  RW> file?  Specifically, keywords like:
  13.  RW>
  14.  RW> USER=John Doe
  15.  
  16. #include <stdio.h>
  17. #include <string.h>
  18.  
  19. /* Public Domain, by Jerry Coffin, tested with MSC 7.0 */
  20.  
  21. char *get_config(char *name, FILE *file) {
  22.  
  23.     char buffer[200];
  24.     char a_name[100];
  25.     static char value[100];
  26. /* These lengths are arbitrarily selected, but should
  27.  * be sufficient for most purposes.
  28.  */
  29.     size_t i,len;
  30.  
  31.     rewind(file);
  32.     while (!feof(file)) {
  33.         fscanf(file," %200[^\n]",buffer);
  34.         if (';' == buffer[0] )
  35.             continue;
  36. /* If the line starts with a semicolon, consider it a comment.
  37.  * ( ignore it )
  38.  */
  39.         sscanf(buffer," %99[^ =] = %99[^\n]",a_name,value);
  40.  
  41. /* This simply looks for some characters ending with either
  42.  * whitespace or an equal sign, followed by an equal sign, then
  43.  * some more characters.  Any whitespace at the beginning of the
  44.  * line or on either side of the equal sign will be ignored.
  45.  */
  46.  
  47.         len = strlen(a_name);
  48.         for (i=0;i<len;i++)
  49.             if ('_' == a_name[i] )
  50.                 memmove(a_name+i,a_name+i+1,len-i);
  51. /* This strips underscore characters out of the name, so if any
  52.  * are present, they will be ignored.
  53.  */
  54.  
  55.         if (!stricmp(a_name,name))
  56. /* stricmp is non-standard, but most compilers include it.  It
  57.  * simply compares the two strings ignoring case.
  58.  */
  59.             break;
  60.          else value[0] = '\0';
  61. /* Otherwise, make the string empty in case it's the last in the
  62.  * file and there were no matches.
  63.  */
  64.     }
  65.     return value;
  66. }
  67.  
  68. #ifdef TEST
  69.  
  70. int main(void) {
  71.  
  72.     FILE *config;
  73.     char *name;
  74.  
  75.     if (NULL == (config =fopen("test.cfg","r" )))
  76.         fprintf(stderr,"\nUnable to open configuration file");
  77.  
  78.     name = get_config("name",config);
  79.     if (name[0])
  80.         printf("my name is %s", get_config("name",config));
  81.     else
  82.         printf("I'm in bad shape - I can't even find my own name");
  83.     fclose(config);
  84.     return 0;
  85. }
  86. #endif
  87.  
  88. ; test.cfg
  89. name=Jerry Coffin
  90.     Later,
  91.     Jerry.
  92.  
  93. --- PPoint 1.60  "The Universe is a figment of its own imagination."
  94.  * Origin: Point Pointedly Pointless (1:128/77.3)
  95. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  96. SEEN-BY: 153/752 154/40 77 157/110 159/100 125 430 575 950 203/23 209/209
  97. SEEN-BY: 261/1023 280/1 390/1 396/1 5 15 2270/1 2440/5 3603/20
  98.